package com.spring.component.utils;

import static com.spring.component.xml.Dom4Util.getBeanForXmlNode;
import static com.spring.component.xml.Dom4Util.xmlSubToBean;

import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import com.spring.component.test.xml.Goods;
import com.spring.component.test.xml.Order;
import com.spring.component.test.xml.OrderBody;
import com.spring.component.test.xml.test.Head;
import com.spring.component.test.xml.test.Request;
import com.spring.component.xml.Dom4Util;
import com.spring.component.xml.XmlAttribleSub;

/**
 * @ClassName: XmlTestUtil
 * @Description:
 * @author: liushihua
 * @date: 2018/12/10
 **/
public class XmlTestUtil {
  private String xml;

  @Before
  public void before() {
    xml =
        "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Orders><Order user=\"张三\" date=\"2008-11-14\" price=\"12279\"><goods id=\"1\"><name>IBM笔记本</name><price>8999</price><count>1</count><totalPrice>8999</totalPrice></goods><goods id=\"2\"><name>雅戈尔西服</name><price>1300</price><count>2</count><totalPrice>2600</totalPrice></goods></Order></Orders>";
    // xml = "<?xml version=\"1.0\"
    // encoding=\"utf-8\"?><REQUEST><HEAD><CODE>CRMOP</CODE><SID>111111</SID><TIMESTAMP>20181224152342</TIMESTAMP><SERVICEID>JXNGBOSS</SERVICEID></HEAD><BODY><OPERATORID>91110022</OPERATORID><OPERATORIP>10.239.52.37</OPERATORIP><MODIFYMODE>add</MODIFYMODE><AUTHORLIST><BOSecRoleGrantBean><opId>99999</opId><orgId>999999</orgId><state>1</state><createDate>2018-12-24
    // 15:23:42</createDate><privId>1</privId><entExpireDate>2099-01-01
    // 00:00:00</entExpireDate><expireDate>2099-01-01
    // 00:00:00</expireDate><upperLimit>1</upperLimit><lowerLimit>9999</lowerLimit><roleGrantId>69163582</roleGrantId><roleId>69163582</roleId><entType>F</entType><validDate>2018-12-24
    // 15:23:42</validDate><entValidDate>2018-12-24
    // 15:23:42</entValidDate><ext1/><ext2/><entId>10000002</entId><notes>CRM运维平台工单操作</notes><WORKNO>20181224150726151</WORKNO></BOSecRoleGrantBean><BOSecRoleGrantBean><opId>99999</opId><orgId>999999</orgId><state>1</state><createDate>2018-12-24
    // 15:23:42</createDate><privId>1</privId><entExpireDate>2099-01-01
    // 00:00:00</entExpireDate><expireDate>2099-01-01
    // 00:00:00</expireDate><upperLimit>1</upperLimit><lowerLimit>9999</lowerLimit><roleGrantId>69163582</roleGrantId><roleId>69163582</roleId><entType>F</entType><validDate>2018-12-24
    // 15:23:42</validDate><entValidDate>2018-12-24
    // 15:23:42</entValidDate><ext1/><ext2/><entId>10000006</entId><notes>CRM运维平台工单操作</notes><WORKNO>20181224150726151</WORKNO></BOSecRoleGrantBean><BOSecRoleGrantBean><opId>99999</opId><orgId>999999</orgId><state>1</state><createDate>2018-12-24
    // 15:23:42</createDate><privId>1</privId><entExpireDate>2099-01-01
    // 00:00:00</entExpireDate><expireDate>2099-01-01
    // 00:00:00</expireDate><upperLimit>1</upperLimit><lowerLimit>9999</lowerLimit><roleGrantId>69163582</roleGrantId><roleId>69163582</roleId><entType>F</entType><validDate>2018-12-24
    // 15:23:42</validDate><entValidDate>2018-12-24
    // 15:23:42</entValidDate><ext1/><ext2/><entId>10000007</entId><notes>CRM运维平台工单操作</notes><WORKNO>20181224150726151</WORKNO></BOSecRoleGrantBean></AUTHORLIST></BODY></REQUEST>";
  }

  @Test
  public void testXPathAttribleSub() throws Exception {
    // XmlAttribleSub xmlAttribleSub = Dom4Util.xPathAttribleSub(xml);
    List<XmlAttribleSub> xmlAttribleSub =
        Dom4Util.xPathAttribleSub(xml, "//Orders/Order/goods/name");
    System.out.println(xmlAttribleSub);
  }

  @Test
  public void testGetNodeValue() throws Exception {
    System.out.println(Dom4Util.getNodeValue(xml, "name", 3, 2));
  }

  @Test
  public void testGetBeanForXml() throws Exception {
    // List<Goods> goods = Dom4Util.getBeanForXmlNode(xml, "//Orders/Order/goods", Goods.class);
    // System.out.println(goods);
    System.out.println(getBeanForXmlNode(xml, "//Orders/Order", Order.class));
  }

  @Test
  public void testGetBeanForXxmlAttr() throws Exception {
    List<Order> orders = Dom4Util.getBeanForXxmlAttr(xml, "//Orders/Order", Order.class);
    System.out.println(orders);
  }

  @Test
  public void testGetNodeAttrValue() throws Exception {
    System.out.println(Dom4Util.getNodeAttrValue(xml, "id", 2, 1));
  }

  @Test
  public void testGetNodeValueX() throws Exception {
    System.out
        .println(CommonUtils.arrToStr(Dom4Util.getNodeValue(xml, "//Orders/Order/goods/name")));
  }

  @Test
  public void testCreateXml() {
    String header = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
    XmlAttribleSub sub0 = new XmlAttribleSub(0, "Orders");
    XmlAttribleSub sub1 = new XmlAttribleSub(1, "Order");
    sub1.addAttr("user", "张三");
    sub1.addAttr("date", "2008-11-14");
    sub1.addAttr("price", "12279");
    XmlAttribleSub sub21 = new XmlAttribleSub(2, "goods");
    sub21.addAttr("id", "1");
    XmlAttribleSub sub31 = new XmlAttribleSub(3, "name");
    sub31.setValue("IBM笔记本");
    XmlAttribleSub sub32 = new XmlAttribleSub(3, "price");
    sub32.setValue("8999");
    XmlAttribleSub sub33 = new XmlAttribleSub(3, "count");
    sub33.setValue("1");
    XmlAttribleSub sub34 = new XmlAttribleSub(3, "totalPrice");
    sub34.setValue("8999");
    XmlAttribleSub sub22 = new XmlAttribleSub(2, "goods");
    sub22.addAttr("id", "2");
    XmlAttribleSub sub35 = new XmlAttribleSub(3, "name");
    sub35.setValue("雅戈尔西服");
    XmlAttribleSub sub36 = new XmlAttribleSub(3, "price");
    sub36.setValue("1300");
    XmlAttribleSub sub37 = new XmlAttribleSub(3, "count");
    sub37.setValue("2");
    XmlAttribleSub sub38 = new XmlAttribleSub(3, "totalPrice");
    sub38.setValue("2600");
    sub0.addSubNode(sub1);
    sub1.addSubNode(sub21);
    sub1.addSubNode(sub22);
    sub21.addSubNode(sub31);
    sub21.addSubNode(sub32);
    sub21.addSubNode(sub33);
    sub21.addSubNode(sub34);
    sub22.addSubNode(sub35);
    sub22.addSubNode(sub36);
    sub22.addSubNode(sub37);
    sub22.addSubNode(sub38);
    System.out.println(header + sub0);
  }

  @Test
  public void testBeanToXmlSub() throws Exception {
    Goods goods = new Goods();
    goods.setName("IBM笔记本");
    goods.setCount(1);
    goods.setPrice("8999");
    goods.setTotalPrice("8999");
    System.out.println(Dom4Util.beanToXmlSub(goods, 2));
    System.out.println(Dom4Util.beanToXmlSub(goods));
    Order order = new Order();
    order.setUser("张三");
    order.setDate(new Date());
    order.setPrice("12279");
    List<Goods> goodsList = new ArrayList<>();
    goodsList.add(goods);
    Goods g = new Goods();
    g.setId("2");
    g.setName("雅戈尔西服");
    g.setPrice("1300");
    g.setCount(2);
    g.setTotalPrice("2600");
    goodsList.add(g);
    order.setGoodsList(goodsList);
    XmlAttribleSub root = Dom4Util.createRoot("Oreders");
    root.addSubNode(Dom4Util.beanToXmlSub(order));
    System.out.println(root);
  }

  @Test
  public void test() throws Exception {
    Class clazz = Goods.class;
    Object obj = clazz.newInstance();
    Field field = clazz.getDeclaredField("count");
    field.setAccessible(true);
    System.out.println(field.getGenericType().getTypeName() instanceof String);
    field.set(obj, 1);
    System.out.println(obj);
    System.out.println(Object.class == Object.class);
    System.out.println(Goods.class.getName().equals(Object.class.getName()));
    System.out.println(Object.class.getName());
    System.out.println(Object.class.getTypeName());
    System.out.println(Object.class.getSimpleName());
  }

  @Test
  public void testXmlSubToBean() throws Exception {
    XmlAttribleSub sub1 = new XmlAttribleSub(1, "Order");
    sub1.addAttr("user", "张三");
    sub1.addAttr("date", "2008-11-14");
    sub1.addAttr("price", "12279");
    XmlAttribleSub sub21 = new XmlAttribleSub(2, "goods");
    sub21.addAttr("id", "1");
    XmlAttribleSub sub31 = new XmlAttribleSub(3, "name");
    sub31.setValue("IBM笔记本");
    XmlAttribleSub sub32 = new XmlAttribleSub(3, "price");
    sub32.setValue("8999");
    XmlAttribleSub sub33 = new XmlAttribleSub(3, "count");
    sub33.setValue("1");
    XmlAttribleSub sub34 = new XmlAttribleSub(3, "totalPrice");
    sub34.setValue("8999");
    XmlAttribleSub sub22 = new XmlAttribleSub(2, "goods");
    sub22.addAttr("id", "2");
    XmlAttribleSub sub35 = new XmlAttribleSub(3, "name");
    sub35.setValue("雅戈尔西服");
    XmlAttribleSub sub36 = new XmlAttribleSub(3, "price");
    sub36.setValue("1300");
    XmlAttribleSub sub37 = new XmlAttribleSub(3, "count");
    sub37.setValue("2");
    XmlAttribleSub sub38 = new XmlAttribleSub(3, "totalPrice");
    sub38.setValue("2600");
    sub1.addSubNode(sub21);
    sub1.addSubNode(sub22);
    sub21.addSubNode(sub31);
    sub21.addSubNode(sub32);
    sub21.addSubNode(sub33);
    sub21.addSubNode(sub34);
    sub22.addSubNode(sub35);
    sub22.addSubNode(sub36);
    sub22.addSubNode(sub37);
    sub22.addSubNode(sub38);
    System.out.println(sub1);
    Order o = xmlSubToBean(sub1, Order.class);
    System.out.println(o);
  }

  @Test
  public void testOrderBody() throws Exception {
    OrderBody orderBody = new OrderBody();
    orderBody.setDate(new Date());
    orderBody.setUserId(1111);
    orderBody.setUserName("王二麻子");

    List<Order> orders = new ArrayList<>();
    Order order = new Order();
    order.setDate(new Date());
    order.setPrice("123456");
    order.setUser("张三");
    List<Goods> goodsList = new ArrayList<>();
    Goods goods = new Goods();
    goods.setName("java从入门到放弃");
    goods.setCount(1);
    goods.setId("1");
    goods.setPrice("1100");
    goods.setTotalPrice("100");
    goodsList.add(goods);
    Goods good = new Goods();
    good.setName("脊椎康复指南");
    good.setCount(1);
    good.setId("1");
    good.setPrice("1000");
    good.setTotalPrice("1000");
    goodsList.add(good);
    order.setGoodsList(goodsList);
    orders.add(order);
    Order o = new Order();
    o.setUser("阿珍爱上了阿强");
    o.setPrice("11000");
    o.setDate(new Date());
    Goods g = new Goods();
    ReflectUtil.copyProperties(g, goods);
    o.setGoodsList(Arrays.asList(g));
    orders.add(o);
    orderBody.setOrders(orders);

    Head head = new Head();
    head.setCode("0000");
    head.setId("11111");
    head.setServiceId("serviceID");
    head.setTimestamp(new Timestamp(System.currentTimeMillis()));

    Request request = new Request();
    request.setHead(head);
    request.setBody(orderBody);

    XmlAttribleSub sub = Dom4Util.beanToXmlSub(request);
    System.out.println(sub);
  }
}
